home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / network / nsclilib / ni_macdv.c < prev    next >
Text File  |  1996-07-05  |  13KB  |  380 lines

  1. /*      
  2. * ===========================================================================
  3. *
  4. *                            PUBLIC DOMAIN NOTICE                          
  5. *               National Center for Biotechnology Information
  6. *                                                                          
  7. *  This software/database is a "United States Government Work" under the   
  8. *  terms of the United States Copyright Act.  It was written as part of    
  9. *  the author's official duties as a United States Government employee and 
  10. *  thus cannot be copyrighted.  This software/database is freely available 
  11. *  to the public for use. The National Library of Medicine and the U.S.    
  12. *  Government have not placed any restriction on its use or reproduction.  
  13. *                                                                          
  14. *  Although all reasonable efforts have been taken to ensure the accuracy  
  15. *  and reliability of the software and data, the NLM and the U.S.          
  16. *  Government do not and cannot warrant the performance or results that    
  17. *  may be obtained by using this software or data. The NLM and the U.S.    
  18. *  Government disclaim all warranties, express or implied, including       
  19. *  warranties of performance, merchantability or fitness for any particular
  20. *  purpose.                                                                
  21. *                                                                          
  22. *  Please cite the author in any work or product based on this material.   
  23. *
  24. * ===========================================================================
  25. *
  26. * File Name:    ni_macdv.c
  27. *
  28. * Author:       epstein
  29. *
  30. * Version Creation Date:        1/13/95
  31. *
  32. * $Revision: 4.0 $
  33. *
  34. * File Description: 
  35. *   Determine which Macintosh MacTCP device is in-use (e.g., "Ethernet", "PPP")
  36. *
  37. * Note: This file is almost entirely based upon source code kindly provided
  38. *       by Robert S. Mah (rmah@panix.com).  This in turn is based upon
  39. *       the Apple technical document "MacTCP 2.0 LAP Tech Note".
  40. *
  41. *       This file is to be used for Macintosh computers only.
  42. *
  43. *
  44. * Modifications:  
  45. * --------------------------------------------------------------------------
  46. * Date     Name        Description of modification
  47. * -------  ----------  -----------------------------------------------------
  48. *01/17/95  Kans        Add CodeWarrior-compatibility
  49. * ==========================================================================
  50. *
  51. *
  52. * RCS Modification History:
  53. * $Log: ni_macdv.c,v $
  54.  * Revision 4.0  1995/07/26  13:56:32  ostell
  55.  * force revision to 4.0
  56.  *
  57.  * Revision 1.3  1995/05/17  17:52:34  epstein
  58.  * add RCS log revision history
  59.  *
  60. */
  61.  
  62. #include <ncbi.h>
  63. #include <ncbiwin.h>
  64.  
  65. #include <MacTCPCommonTypes.h>
  66. #include <Files.h>
  67. #include <Errors.h>
  68. #include <Resources.h>
  69. #include <Memory.h>
  70. #include <GestaltEqu.h>
  71. #include <Folders.h>
  72.  
  73. #ifdef COMP_METRO
  74. typedef OSErr (*OSErrProcPtr)();
  75. typedef Ptr (*PtrProcPtr)();
  76. typedef Boolean (*BooleanProcPtr)();
  77. typedef void (*voidProcPtr)();
  78.  
  79. #endif
  80.  
  81. typedef unsigned char uchar;
  82.  
  83. extern Boolean ResLoad;
  84.  
  85. #if GENERATINGPOWERPC
  86. #pragma options align=mac68k
  87. #endif
  88.  
  89.  
  90. /*
  91.  * Holds IP and LAP-specific configuration information.  This is held in the
  92.  * 'ipln' configuration resource in either the MacTCP driver or the "MacTCP
  93.  * Prep" files.
  94.  */
  95. typedef struct IPConfig {
  96.         long            version;
  97.         long            flags;
  98.         long            dfl_ip_addr;
  99.         long            dfl_net_mask;
  100.         long            dfl_broadcast_mask;
  101.         long            dfl_gateway_addr;
  102.         b_8                     server_lap_address[8];
  103.         long            configIPAddr;
  104.         long            configNetMask;
  105.         long            dfl_dyn_low;
  106.         long            dfl_dyn_high;
  107.         char            dfl_zone[34];           /* ### */
  108.         Boolean         load;
  109.         Boolean         admin;
  110.         Boolean         netLock;
  111.         Boolean         subnetLock;
  112.         Boolean         nodeLock;
  113.         Boolean         filler1;
  114.         long            activeLap;
  115.         long            slot;
  116.         char            filename[33];           /* ### */
  117. } IPConfig;
  118.  
  119.  
  120. /*
  121.  * LAPInfo is allocated during system start-up and holds all of the
  122. LAP-specific information.
  123.  */
  124. struct LAPInfo {
  125.         b_32                    our_ip_addr;            /* LAP's IP address =
  126. */
  127.         b_32                    our_net_mask;           /* LAP's IP net-mask=
  128.  */
  129.         b_32                    ip_broadcast_addr;      /* IP's broadcast
  130. address */
  131.         IPConfig lc;                                     /* copy of
  132. IP LAP configuration resource */
  133.         OSErrProcPtr    lapInit;                        /* pointer to
  134. once-only LAP init routine */
  135.         OSErrProcPtr    lapOpen;                        /* LAP open routine =
  136. */
  137.         OSErrProcPtr    lapClose;                       /* LAP close routine=
  138.  */
  139.         voidProcPtr             lapUnload;                      /* LAP
  140. unload routine, undoes LapInit */
  141.         OSErrProcPtr    lapAttach;                      /* LAP attach PH
  142. routine */
  143.         OSErrProcPtr    lapDetach;                      /* LAP detach=
  144.  routine */
  145.         OSErrProcPtr    lapOutput;                      /* LAP output=
  146.  routine */
  147.         OSErrProcPtr    lapControl;                     /* LAP control
  148. routine */
  149.         voidProcPtr             lapFault;                       /* LAP
  150. fault isolation routine */
  151.         OSErrProcPtr    lapStatistics;          /* LAP statistic reading
  152. routine */
  153.         voidProcPtr             lapConfigure;           /* LAP-specific
  154. configuration routines */
  155.         BooleanProcPtr  lapProbe;                       /* send a
  156. LAP-specific address probe packet */
  157.         BooleanProcPtr  lapRegister;            /* register our IP address
  158. on the network */
  159.         voidProcPtr             lapFindGateway;         /* LAP-specific
  160. means of finding a gateway */
  161.         BooleanProcPtr  lapGwyCheck;            /* LAP-specific means of
  162. verifying gateway up */
  163.         /* IP parameters */
  164.         ip_addr                 dfl_dns_addr;           /* address of DNS
  165. from config protocol */
  166.         Handle                  dnslHndl;                       /* handle
  167. to DNS configuration resource */
  168.         Ptr                             dnsCache;                       /*
  169. pointer to space allocated for dns cache */
  170.         long                    dnsCacheSize;           /* size of cache
  171. allocated, in bytes */
  172.         /* LAP parameters */
  173.         long                    headerSize;                     /* LAP
  174. header space requirements */
  175.         long                    trailerSize;            /* LAP trailer
  176. space requirements */
  177.         long                    outMaxPacketSize;       /* size of maximum
  178. output packet */
  179.         long                    inMaxPacketSize;        /* size of maximum
  180. input packet */
  181.         long                    maxDataSize;            /* size of maximum
  182. data packet */
  183.         long                    numConnections;         /* number of
  184. separate network connections */
  185.         unsigned long   versionFlags;           /* version number flags */
  186.         voidProcPtr             ip_ph;                          /* pointer
  187. to IP's protocol handler */
  188.         Ptr                             ipGlobals;                      /*
  189. pointer to IP's A5 */
  190.         short                   link_unit;                      /* unit
  191. number of link driver */
  192.         Boolean                 addressConflict;        /* TRUE if address
  193. conflict discovered */
  194.         long                    lapType;                        /* IP LAP
  195. hardware type number */
  196.         long                    lapAddrLength;          /* size of LAP
  197. address field */
  198.         unsigned char   *lapAddrPtr;            /* pointer to LAP address
  199. field */
  200.         unsigned long   reserved;                       /* MacTCP reserved
  201. field */
  202. };
  203.  
  204.  
  205.  
  206.  
  207. #if GENERATINGPOWERPC
  208. #pragma options align=reset
  209. #endif
  210.  
  211.  
  212.  
  213.  
  214. char * GetLAPType( void );
  215. static char * ParseLAPType(IPConfig **configH );
  216. static OSErr SearchFolderForRsrc( FSSpec *target, short vRefNum, long dirID,
  217.                      OSType ftype, OSType fcrea, OSType rsrcType );
  218.  
  219.  
  220. //--------------------------------------------------------------------------
  221. // Returns the current LAP driver type
  222. // Note: We should fix this to search the control panels folder for any file
  223. // of the correct type containing a 'ipln' resource.
  224. //--------------------------------------------------------------------------
  225.  
  226. char * GetLAPType( void )
  227. {
  228.         IPConfig **configH;
  229.         long responce;
  230.         char safety1[100];
  231.         FSSpec fspec;
  232.         char safety2[100];
  233.         short refNum = -1;
  234.         char *lapType = NULL;
  235.  
  236.         if( Gestalt( 'mtcp', &responce ) != noErr )
  237.                 return NULL;
  238.  
  239.         // First check the MacTCP Prep file
  240.         if( FindFolder( kOnSystemDisk, kPreferencesFolderType, false,
  241.                         &fspec.vRefNum, &fspec.parID ) != noErr )
  242.                 goto try_mactcp_drvr;
  243.  
  244.         if( SearchFolderForRsrc( &fspec, fspec.vRefNum, fspec.parID,
  245.                                 'mtpp', 'mtcp', 'ipln' ) != noErr )
  246.                 goto try_mactcp_drvr;
  247.  
  248.         if( (refNum = FSpOpenResFile( &fspec, fsRdPerm )) == -1 )
  249.                 goto try_mactcp_drvr;
  250.  
  251.         configH = (IPConfig**) Get1IndResource( 'ipln', 1 );
  252.         if( configH != NULL && *configH != NULL ){
  253.                 lapType = ParseLAPType( configH );
  254.         }
  255.         CloseResFile( refNum );
  256.  
  257.         if( lapType != NULL )
  258.                 return lapType;
  259.  
  260.         // Otherwise check the MacTCP driver/control panel
  261.  
  262. try_mactcp_drvr:
  263.         if( FindFolder( kOnSystemDisk, 'cntl', false,
  264.                          &fspec.vRefNum, &fspec.parID ) != noErr )
  265.                 goto exit;
  266.  
  267.         if( SearchFolderForRsrc( &fspec, fspec.vRefNum, fspec.parID,
  268.                                 'cdev', 'ztcp', 'ipln' ) != noErr )
  269.                 goto exit;
  270.  
  271.         if( (refNum = FSpOpenResFile( &fspec, fsRdPerm )) == -1 )
  272.                 goto exit;
  273.  
  274.         configH = (IPConfig**) Get1IndResource( 'ipln', 1 );
  275.         if( configH != NULL && *configH != NULL ){
  276.                 lapType = ParseLAPType( configH );
  277.         }
  278.         CloseResFile( refNum );
  279.  
  280. exit:
  281.         return lapType;
  282. }
  283.  
  284.  
  285.  
  286. //--------------------------------------------------------------------------
  287. // Takes a 'ipln' resource and tries to figure out which LAP driver we are
  288. // using.
  289. //--------------------------------------------------------------------------
  290.  
  291. static char * ParseLAPType(IPConfig **configH )
  292. {
  293.         const IPConfig *ipc;
  294.         long    result;
  295.         static  char   laptype[256];
  296.         uchar   hs;
  297.  
  298.         hs = HGetState( (Handle) configH );
  299.         HLock( (Handle) configH );
  300.         ipc = *configH;
  301.  
  302.         // Check if MacTCP is using LocalTalk or Ethernet
  303.         if( ipc->activeLap == 128 || ipc->activeLap == 129 ){
  304.                 if (ipc->activeLap == 128)
  305.                         return "LocalTalk";
  306.                 else
  307.                         return "Ethernet";
  308.         }
  309.  
  310.         memcpy(laptype, ipc->filename, ipc->filename[0] + 1);
  311.         p2cstr((unsigned char *)laptype);
  312.         
  313.         return laptype;
  314. }
  315.  
  316.  
  317.  
  318. //--------------------------------------------------------------------------
  319. // Search a folder for a file of a specific type and creator (specify 0L for
  320. // any) that contains a resource of a given type.
  321. // Returns a FSpec to that file.
  322. //--------------------------------------------------------------------------
  323.  
  324. static OSErr
  325. SearchFolderForRsrc( FSSpec *target, short vRefNum, long dirID,
  326.                      OSType ftype, OSType fcrea, OSType rsrcType )
  327. {
  328.     FSSpec              fspec;
  329.     HParamBlockRec      fi;
  330.     Handle              rsrcH;
  331.     short               refNum;
  332.     OSErr               err;
  333.     Boolean             oldResLoad;
  334.  
  335.     err = fnfErr;
  336. #ifdef NEW_UNIVERSAL_HEADERS /* JAE */
  337.     oldResLoad = LMGetResLoad();
  338. #else
  339.     oldResLoad = /* ResLoad */ true;
  340. #endif /* NEW_UNIVERSAL_HEADERS */
  341.     SetResLoad( false );
  342.  
  343.     fspec.vRefNum = vRefNum;
  344.     fspec.parID = dirID;
  345.  
  346.     fi.fileParam.ioCompletion = NULL;
  347.     fi.fileParam.ioNamePtr = fspec.name;
  348.     fi.fileParam.ioVRefNum = vRefNum;
  349.     fi.fileParam.ioDirID = dirID;
  350.     fi.fileParam.ioFDirIndex = 1;
  351.  
  352.     while( PBHGetFInfoSync( &fi ) == noErr )
  353.     {
  354.         // scan folder for resource files of specific type & creator
  355.         if( (fcrea == 0L || fi.fileParam.ioFlFndrInfo.fdCreator == fcrea)
  356.             && (ftype == 0L || fi.fileParam.ioFlFndrInfo.fdType == ftype) )
  357.         {
  358.             // found the conforming file, try to open it
  359.             if( (refNum=FSpOpenResFile(&fspec,fsRdPerm)) != -1 )
  360.             {
  361.                 // see if a resource is there
  362.                 rsrcH = GetIndResource( rsrcType, 1 );
  363.                 CloseResFile( refNum );
  364.  
  365.                 // if there, return noErr
  366.                 if( rsrcH != NULL ){
  367.                     err = noErr;
  368.                     *target = fspec;
  369.                     break;
  370.                 }
  371.             }
  372.         }
  373.         // check next file in folder
  374.         fi.fileParam.ioFDirIndex++;
  375.         fi.fileParam.ioDirID = dirID;  // PBHGetFInfo() clobbers ioDirID
  376.     }
  377.     SetResLoad( oldResLoad );
  378.     return err;
  379. }
  380.